package testsuite;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import formatter.TopCoderFormatter;

/**
 * A base class for all test case generators for TopCoder MPSQAS
 *
 * Reminder: You must close TestSuite after every tests
 * 
 * @author dolphinigle
 */
public abstract class TopCoderTestSuite extends TestSuite {

  /**
   * Filename where to write the cases to
   */
  private String filename_;

  /**
   * BufferedWriter of this.getFilename();
   */
  private TopCoderFormatter formatter_;

  /**
   * Constructs a test suite that's responsible for
   * @param filename name of file to where the result is to be written
   * @throws IOException if opening file to write failes
   */
  public TopCoderTestSuite(String filename) {
    this.filename_ = filename;
    try {
      this.formatter_ = new TopCoderFormatter(new BufferedWriter(new FileWriter(filename)));
    } catch (IOException ex) {
      throw new Error(ex.toString());
    }
  }

  /**
   * Returns the filename to where this test suit is writing
   * @return the filename
   */
  public String getFilename() {
    return this.filename_;
  }

  /**
   * Return the TopCoderFormatter to where this test suite writes to
   * @return the formatter
   */
  protected TopCoderFormatter getFormatter() {
    return this.formatter_;
  }

  /**
   * Generates a number of random test cases depending on the suit and write
   * them to the stream
   * @param number_of_tests rough guide on the number of tests. Generally
   * larger means stronger tests. However this is not a strict guide.
   * some implementations ignore this parameter, which is 100% allowed.
   */
  public abstract void generateRandomCases(int number_of_tests);

  /**
   * Closes the file writer and finally complete writing to the file
   * Must be called after the suite is completed
   */
  public void close() {
    try {
      this.formatter_.close();
    } catch (IOException ex) {
      System.err.println(ex.toString());
    }
  }

}
